package extras.mak001;

import java.awt.event.KeyEvent;

import com.rsbuddy.script.methods.Keyboard;
import com.rsbuddy.script.methods.Npcs;
import com.rsbuddy.script.methods.Objects;
import com.rsbuddy.script.task.LoopTask;
import com.rsbuddy.script.util.Filter;
import com.rsbuddy.script.util.Random;
import com.rsbuddy.script.wrappers.GameObject;
import com.rsbuddy.script.wrappers.Npc;

public class AntiBan extends LoopTask {

	private static String antiStatus = "";
	private static boolean doing = false;;
	private static boolean canDo = false;

	public int loop() {
		if (canDo == true) {
			switch (Random.nextInt(0, 50)) {
			case 0:
				antiStatus = "Case 0: Rotatig screen";
				doing = true;
				rotateScreen(5, 1000);
				doing = false;
				sleep(Random.nextInt(getMin(15000), getMax(15000)));
				break;

			case 14:
				antiStatus = "Case 14: Examining Object";
				doing = true;
				examineOBJ();
				doing = false;
				sleep(Random.nextInt(getMin(15000), getMax(15000)));
				break;

			case 23:
				antiStatus = "Case 23: Examining NPC";
				doing = true;
				examineNPC();
				doing = false;
				sleep(Random.nextInt(getMin(15000), getMax(15000)));
				break;

			case 39:
				antiStatus = "Case 39: Rotatig screen";
				doing = true;
				rotateScreen(200, 700);
				doing = false;
				sleep(Random.nextInt(getMin(15000), getMax(15000)));
				break;

			case 44:
				antiStatus = "Case 44: Examining NPC";
				doing = true;
				examineNPC();
				doing = false;
				sleep(Random.nextInt(getMin(15000), getMax(15000)));
				break;

			case 48:
				antiStatus = "Case 48: Examining Object";
				doing = true;
				examineOBJ();
				doing = false;
				sleep(Random.nextInt(getMin(15000), getMax(15000)));
				break;

			}
		} else {
			sleep(500);
		}
		// TODO Auto-generated method stub
		return randomNext(15000);
	}

	private static int getMin(int i) {
		if (i > 100) {
			if (i > 200) {
				return i - 200;
			} else {
				return i - 100;
			}
		} else {
			return i;
		}
	}

	private static int getMax(int i) {
		return Random.nextInt(i, i + 30000);
	}

	private static int randomNext(int i) {
		return Random.nextInt(getMin(i), getMax(i));
	}

	private static void rotateScreen(int i, int j) {
		char key = KeyEvent.VK_RIGHT;
		if (5 > Random.nextInt(Random.nextInt(0, 5), Random.nextInt(5, 9)))
			key = KeyEvent.VK_LEFT;
		Keyboard.pressKey(key);
		Random.nextInt(i, j);
		Keyboard.releaseKey(key);
	}

	private static void examineOBJ() {
		Objects.getNearest(new Filter<GameObject>() {
			@Override
			public boolean accept(GameObject o) {
				return o.getType().equals(GameObject.Type.INTERACTIVE)
						&& o.isOnScreen();
			}
		}).interact("Examine");
	}

	private static void examineNPC() {
		Npcs.getNearest(new Filter<Npc>() {
			@Override
			public boolean accept(Npc o) {
				return o.isOnScreen();
			}
		}).interact("Examine");
	}

	public static boolean isDoing() {
		return doing;
	}

	public static String getState() {
		return antiStatus;
	}

	public static void setAntiBan(boolean b) {
		canDo = b;
	}
}
